<template>
  <div class="layout-all">
    <el-row>
      <el-col :span="12">
        <el-form ref="form" :rules="rules" :model="form" label-width="130px">
          <el-form-item label="轮播图名:" prop="title">
            <el-input v-model="form.title" placeholder="请输入广告名" style="width: 70%;"/>
          </el-form-item>
          <el-form-item label="轮播位置:" prop="type">
            <el-select v-model="form.type" aria-placeholder="请选择" style="width: 70%;">
              <el-option
                v-for="item in type_option"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="封面:" prop="thumb">
            <el-upload
              class="avatar-banner"
              list-type="picture-card"
              :action="upload_url"
              :show-file-list="false"
              :before-upload="$beforeAvatarUploadMb"
              :on-success="handleAvatarSuccess_thumb"
            >
              <img v-if="thumb" :src="thumb" class="avatar">
              <i v-else class="el-icon-plus" />
            </el-upload>
            <div style="font-size:12px;">建议尺寸：350px X 150px</div> 
          </el-form-item>
          <el-form-item label="url地址:" prop="url" >
            <el-input v-model="form.url" placeholder="请输入url地址" style="width: 70%;" />
          </el-form-item>
          <el-form-item label="排序:" prop="sort">
            <el-input v-model="form.sort" placeholder="请输入排序" type="number" style="width: 70%;" />
          </el-form-item>
          <el-form-item label="是否启用:" prop="status">
            <el-switch
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
            />
          </el-form-item>

          <el-form-item>
            <el-button icon="el-icon-close" circle @click="bk()" />
            <el-button
              type="success"
              icon="el-icon-check"
              circle
              @click="onSubmit('form')"
            />
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="12">
        <div>选择跳转地址</div>  
        <url-address @selectMenu="selectMenu" @selectActivity="selectActivity"></url-address>
      </el-col>
    </el-row>
    
  </div>
</template>

<script>
import urlAddress from '../../components/url_address/urlAddress'
export default {
  components: { urlAddress },
  data() {
    return {
      type_option: [{ value: 0, label: '首页' }],
      id: 0,
      upload_url: '',
      thumb: '',
      form: {
        id: 0,
        sort: 0,
        type: 0,
        status: true,
        title: '',
        url: '',
        thumb: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入轮播图名', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择轮播位置', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    if (this.$route.query.id) {
      this.id = this.$route.query.id
    }
    this.upload_url = process.env.VUE_APP_BASE_API + '/pub/Upload/upload'
    this.init_into()
  },
  methods: {
     selectMenu (item) {
        this.form.url = item.path
    },
    selectActivity (url) {
      this.form.url = url
    },
    // 封面上传完成
    handleAvatarSuccess_thumb(res, file) {
      this.form.thumb = file.response.data.path
      this.thumb = file.response.data.path
    },
    // 限制图片大小格式
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 提交
    onSubmit(formName) {
      const form = this.form
      console.log(form)
      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          form.status === true ? form.status = 1 : form.status = 0
          this.get('/admin/Banner/save', form, 'POST').then(e => {})
          this.$router.back()
        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },
    // 返回
    bk() {
      this.$router.back()
    },
    // 获取当前数据
    init_into() {
      this.get('/admin/Banner/info?id=' + this.id, 'POST').then(e => {
        const tpl = e.info.tpl
        tpl.status === 1 || tpl.status === '' ? this.form.status = true : this.form.status = false
        this.form.id = tpl.id
        tpl.sort ? (this.form.sort = tpl.sort) : ''
        this.form.title = tpl.title
        this.form.url = tpl.url
        this.form.thumb = tpl.thumb
        this.thumb = tpl.thumb
        console.log(this.form)
      })
    }
  }
}
</script>

<style>
.layout-all {
  margin: 10px;
}
.avatar-banner {
  width:120px;
  height:52px;
}
.avatar-banner .el-upload--picture-card {
  width:120px;
  height:52px;
}
.avatar-banner .el-upload--picture-card img {
    width:117px;
  height:50px;
}
</style>
